﻿using BsForApi.Model;
using BsForApi.Model.Account;
using BsForApi.Tool;
using HCDV.HCSP;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BsForApi
{
    /// <summary>
    /// 基本信息
    /// </summary>
    public class BaseYwInfo
    {
        /// <summary>
        /// 根据登录key获取用户信息
        /// </summary>
        /// <param name="loginKye"></param>
        /// <returns></returns>
        public static ApiOut<LoginOut> getUserInfoByKey(string loginKey, string userid = null)
        {
            ApiOut<LoginOut> re = new ApiOut<LoginOut>();
            DataTable user;
            using (dbsimple db = new dbsimple())
            {
                if (string.IsNullOrEmpty(userid))
                    user = db.Query("select logininfo  from mrbaseinf where loginkey='" + loginKey + "'");
                else
                { user = db.Query("select logininfo  from mrbaseinf where EmpID=" + userid); }
                if (user == null || user.Rows.Count == 0)
                {
                    re.SetError("登录信息无效，请重新登录");
                    return re;
                }
                else
                {
                    var linfo = user.Rows[0][0].ToString();
                    if (string.IsNullOrEmpty(linfo))
                    {
                        re.SetError("服务器，没保存用户信息");
                        return re;
                    }
                    else
                    {
                        re.Info = JsonConvert.DeserializeObject<LoginOut>(linfo);
                        return re;
                    }
                }
            }

        }

        /// <summary>
        /// 根据登录key获取用户信息
        /// </summary>
        /// <param name="loginKye"></param>
        /// <returns></returns>
        public static ApiOut<List<UserInfo>> GetBranchUser(string loginKey)
        {
            ApiOut<List<UserInfo>> re = new ApiOut<List<UserInfo>>();
            ApiOut<LoginOut> user = getUserInfoByKey(loginKey);
            if (!user.State)
            {
                re.SetError(user.Desc);
                return re;
            }
            using (dbsimple db = new dbsimple())
            {
                var dt = db.Query(@"select empid  as id,concat(depname,'-',empname) as name  from view_user  where branchid='" + user.Info.UserOrgId + "'");
                re.Info = (List<UserInfo>)ModelConvertHelper<UserInfo>.ConvertToModel(dt);
            }
            return re;
        }



        /// <summary>
        /// 登录处理
        /// </summary>
        /// <param name="userinfo"></param>
        /// <returns></returns>
        public static ApiOut<LoginOut> Login(Loginin userinfo)
        {
            ApiOut<LoginOut> re = new ApiOut<LoginOut>();
            DataTable user;
            Hashtable allinfo = new Hashtable();
            allinfo.Add("userid", userinfo.UserAccount);
            using (dbsimple db = new dbsimple())
            {
                user = db.getProc("getuserbylid", allinfo).Tables[0];
            }
            if (user == null || user.Rows.Count == 0)
            {
                re.SetError("账号不存在");
            }
            else
            {
                DataRow row = user.Rows[0];
                if (row["password"].ToString().Equals(userinfo.UserPassWord))
                {
                    LoginOut lu = new LoginOut();
                    lu.UserId = row["empid"].ToString();
                    // lu.OrgCode = row["SimpleCode"].ToString();
                    lu.UserName = userinfo.UserAccount;
                    lu.UserDepId = row["DepId"].ToString();
                    lu.UserOrgId = row["branchId"].ToString();
                    lu.BranchType = row["branchType"].ToString();
                    lu.UserTitle = row["roles"].ToString();
                    lu.UserDepName = row["depname"].ToString();
                    lu.UserOrgName = row["branchname"].ToString();
                    lu.LoginKey = Guid.NewGuid().ToString();
                    using (dbsimple db = new dbsimple())
                    {
                        db.SpExe(string.Format("update mrbaseinf set  loginTime='{1}',loginkey='{2}',logininfo='{3}' where EmpID={0}",
                            lu.UserId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), lu.LoginKey, JsonConvert.SerializeObject(lu)
                            ));
                    }
                    re.Info = lu;

                }
                else
                {
                    re.SetError("密码不正确");
                }
            }
            return re;


        }
    }
}
